home *** CD-ROM | disk | FTP | other *** search
/ Gold Medal Software 2 / Gold Medal Software Volume 2 (Gold Medal) (1994).iso / os2 / cenvi2.arj / CMM_VS_C.DOC < prev    next >
Text File  |  1993-12-21  |  24KB  |  538 lines

  1.                     CEnvi Shareware Manual, Chapter 3:
  2.                       Cmm versus C, for C Programmers
  3.  
  4.  
  5.                      CEnvi unregistered version 1.008
  6.                              21 December 1993
  7.  
  8.                        CEnvi Shareware User's Manual
  9.  
  10.           Copyright 1993, Nombas, All Rights Reserved.
  11.           Published by Nombas, P.O. Box 875, Medford, MA 02155 USA
  12.           (617)391-6595
  13.  
  14.           Thank you for trying this shareware version of CEnvi from Nombas,
  15.           a member of the Association of Shareware Professionals (ASP).
  16.  
  17. 3.  Cmm versus C: The Cmm language for C programmers
  18.  
  19.           This chapter is for those who already know how to program in the
  20.           C language.  This chapter describes only those elements of Cmm
  21.           that differ from standard C, and so if you don't already
  22.           understand C, then this shouldn't have much meaning for you.
  23.           Non-C programmers should instead look at the previous chapter.
  24.  
  25.           Since it is assumed that readers of this chapter are already
  26.           knowledgeable in C, only those aspects of Cmm that differ from C
  27.           are described here.  If it's not mentioned here, then assume that
  28.           Cmm behavior will be standard C.
  29.  
  30.           Deviations from C are a result of these two harmonious Cmm
  31.           directives: Convenience and Safety.  Cmm is different from C
  32.           where the change makes Cmm more convenient for small programs,
  33.           command-line code, or scripting files, or if unaltered C rules
  34.           encourage coding that is potentially unsafe.
  35.  
  36. 3.1.  C Minus Minus
  37.  
  38.           Cmm is "C minus minus" where the minuses are Type Declarations
  39.           and Pointers.  If you already know C and can remember to forget
  40.           these two aspects of C (I repeat, no Type Declarations and no
  41.           Pointers) then you know Cmm.  If you were to take C code, and
  42.           delete all the lines, code-words, and symbols that either declare
  43.           data types or explicitly point to data, then you would be left
  44.           with Cmm code; and although you would be removing bytes of source
  45.           code, you would not be removing capabilities.
  46.  
  47.           All of the details below that compare Cmm against C follow from
  48.           the general rule:
  49.             *Cmm is C minus Type Declarations and minus Pointers.
  50.  
  51. 3.2.  Data Types
  52.  
  53.           The only recognized data types are Float, Long, and Byte.  The
  54.           words "Float", "Long", and "Byte" do not appear in Cmm source
  55.           code; instead, the data types are determined by context.  For
  56.           instance 6 is a Long, 6.6 is a Float, and '6' is a Byte.  Byte is
  57.           unsigned, and the other types are signed.
  58.  
  59. 3.3.  Automatic Type Declaration
  60.  
  61.           There are no type declarators and no type casting.  Types are
  62.           determined from context.  If the code says "i=6" then i is a
  63.           Long, unless a previous statement has indicated otherwise.
  64.  
  65.           For instance, this C code:
  66.               int Max(int a,int b)
  67.               {
  68.                 int result;
  69.                 result = ( a < b ) ? b : a ;
  70.                 return result;
  71.               }
  72.           could become this Cmm code:
  73.               Max(a,b)
  74.               {
  75.                 result = ( a < b ) ? b : a ;
  76.                 return result;
  77.               }
  78.  
  79. 3.4.  Array Representation
  80.  
  81.           Arrays are used in Cmm much like they are in C, except that they
  82.           are stored differently: a first-order array (e.g., a string) is
  83.           stored in consecutive bytes in memory, but arrays of arrays are
  84.           not in consecutive memory locations.  The C declaration "char
  85.           c[3][3];" would state that there are nine consecutive bytes in
  86.           memory.  In Cmm a similar statement such as "c[2][2] = 'A'" would
  87.           tell you that there are (at least) three arrays of characters,
  88.           and the third array of arrays has (at least) three characters in
  89.           it; and although the characters in c[0] are in consecutive bytes,
  90.           and the characters in c[1] are in consecutive bytes, the two
  91.           arrays c[0] and c[1] are not necessarily adjacent in memory.
  92.  
  93. 3.4.1   Array Arithmetic
  94.  
  95.           When one array is assigned to the other, as in:
  96.               foo = "cat";
  97.               goo = foo;
  98.           then both variables define the same array and start at the same
  99.           offset 0.  In this case, if foo[2] is changed then you will find
  100.           that goo[2] has also been changed.
  101.  
  102.           Integer addition and subtraction can also be performed on arrays.
  103.           Array addition or subtraction sets where the array is based.  By
  104.           altering the previous code segment to:
  105.               foo = "cat";
  106.               goo = foo + 1;
  107.           goo and foo would now be arrays containing the same data, except
  108.           that now goo is based one element further, and foo[2] is now the
  109.           same data as goo[1].
  110.  
  111.           To demonstrate:
  112.               foo = "cat";  // foo[0] is 'c', foo[1] = 'a'
  113.               goo = foo + 1;// goo[0] is 'a', goo[-1] = 'c'
  114.               goo[0] = 'u'; // goo[0] is 'u', foo[1] = 'u', foo is "cut"
  115.               goo++;        // goo[0] is 't', goo[-2] = 'c'
  116.               goo[-2] = 'b' // goo[0] is 't', foo[0] = 'b', foo is "but"
  117.  
  118. 3.4.2   Automatic Array Allocation
  119.  
  120.           Arrays are dynamic, and any index, (positive or negative) into an
  121.           array is always valid.  If an element of an array is referred to,
  122.           then the Cmm must see to it that such an element will exist.  For
  123.           instance if the first statement in the Cmm source code is "foo[4]
  124.           = 7;" then the Cmm interpreter will make an array of 5 integers
  125.           referred to by the variable foo.  If a statement further on
  126.           refers to "foo[6]" then the Cmm interpreter will grow foo, if it
  127.           has to, to ensure that the element foo[6] exists.  This works
  128.           with negative indices as well.  When you refer to foo[-10], then
  129.           foo is grown in the other direction if it needs to be, but foo[4]
  130.           will still refer to that "7" you put there earlier.  Arrays can
  131.           reach any dimension order, so that foo[6][7][34][-1][4] is a
  132.           valid value.
  133.  
  134. 3.5.  Structures
  135.  
  136.           Structures are created dynamically, and their elements are not
  137.           necessarily contiguous in memory.  When CEnvi comes across the
  138.           statement 'foo.animal = "dog"' it creates a structure element of
  139.           foo that is referred to as "animal" and is an array of
  140.           characters, and this "animal" variable is thereafter carried
  141.           around with "foo" (much like a stem variable in REXX).  The
  142.           resulting code looks very much like regular C code, except that
  143.           there is not a separate structure definition anywhere.
  144.  
  145.           This C code:
  146.  
  147.               struct Point {
  148.                 int Row;
  149.                 int Column;
  150.               };
  151.  
  152.               struct Square {
  153.                 struct Point BottomLeft;
  154.                 struct Point TopRight;
  155.               };
  156.  
  157.               void main()
  158.               {
  159.                 struct Square sq;
  160.                 int Area;
  161.                 sq.BottomLeft.Row = 1;
  162.                 sq.BottomLeft.Column = 15;
  163.                 sq.TopRight.Row = 82;
  164.                 sq.TopRight.Column = 120;
  165.                 Area = AreaOfASquare(sq);
  166.               }
  167.  
  168.               int AreaOfASquare(struct Square s)
  169.               {
  170.                 int width, height;
  171.                 width = s.TopRight.Column - s.BottomLeft.Column + 1;
  172.                 height = s.TopRight.Row - s.BottomLeft.Row + 1;
  173.                 return( length * height );
  174.               }
  175.  
  176.           can be changed into the equivalent Cmm code simply be removing
  177.           declaration lines, resulting in:
  178.  
  179.               main()
  180.               {
  181.                 sq.BottomLeft.Row = 1;
  182.                 sq.BottomLeft.Column = 15;
  183.                 sq.TopRight.Row = 82;
  184.                 sq.TopRight.Column = 120;
  185.                 Area = AreaOfASquare(sq);
  186.               }
  187.  
  188.               int AreaOfASquare(s)
  189.               {
  190.                 width = s.TopRight.Column - s.BottomLeft.Column + 1;
  191.                 height = s.TopRight.Row - s.BottomLeft.Row + 1;
  192.                 return( length * height );
  193.               }
  194.  
  195.           Structures can be passed, returned, and modified just as any
  196.           other variable.  Of course structures and arrays are independent,
  197.           so you could very well have the statement "foo[8].animal.forge[3]
  198.           = bil.bo".
  199.  
  200. 3.6.  Passing Variables by Reference
  201.  
  202.           By default, LValues in Cmm are passed to functions by reference,
  203.           and so if the function alters a variable then the variable in the
  204.           calling function is altered as well IF IT IS AN LVALUE.  So
  205.           instead of this C code:
  206.  
  207.               main() {
  208.                 .
  209.                 .
  210.                 .
  211.                 CQuadrupleInPlace(&i);
  212.                 .
  213.                 .
  214.                 .
  215.               }
  216.  
  217.               void CQuadrupleInPlace(int *j)
  218.               {
  219.                 *j *= 4;
  220.               }
  221.  
  222.           the Cmm version would be:
  223.  
  224.               main() {
  225.                 .
  226.                 .
  227.                 .
  228.                 QuadrupleInPlace(i);
  229.                 .
  230.                 .
  231.                 .
  232.               }
  233.  
  234.               void QuadrupleInPlace((j)
  235.               {
  236.                 j *= 4;
  237.               }
  238.  
  239.           If the rare circumstance arises that you want to pass a copy of
  240.           an LValue to a function, instead of passing the variable by
  241.           reference, then you can use the Cmm "copy-of" operator "=".
  242.           foo(=i) can be interpreted as saying "pass a value equal to i,
  243.           but not i itself"; so that "foo(=i) ... foo(j) { j *= 4; }" would
  244.           not change the value at i.
  245.  
  246.           Note that for this Cmm version, the following calls to
  247.           QuadrupleInPlace() would be valid, but no value will have changed
  248.           upon return from QuadrupleInPlace() because an LValue is not
  249.           being passed:
  250.               QuadrupleInPlace(8);
  251.               QuadrupleInPlace(i+1);
  252.               QuadrupleInPlace(=1);
  253.  
  254. 3.7.  Data Pointers(*) and Addresses(&)
  255.  
  256.           No pointers.  None.  The "*" symbol NEVER means "pointer" and the
  257.           "&" symbol never means "address".  This may at first cause
  258.           seasoned C programmers to gasp in disbelief, but it turns out to
  259.           be not such a big deal, and these two operators are seldom
  260.           missed, after considering these two rules:
  261.               1) "*var" can be replaced in all instances by "var[0]"
  262.               2) variables (if LValues) are passed by reference
  263.  
  264. 3.8.  Case Statements
  265.  
  266.           Case statements in a switch statement may be a constant, a
  267.           variable, or any other statement that can be evaluated to a
  268.           variable.  So you might see this Cmm code:
  269.  
  270.               switch(i) {
  271.                 case 4:
  272.                 case foe():
  273.                 case sqrt(foe()):
  274.                 case (PILLBOX * 3 - 2):
  275.                 default:
  276.               }
  277.  
  278. 3.9.  Initialization: Code external to functions
  279.  
  280.           All code that is not inside any function block is interpreted
  281.           before main() is called.  So the following Cmm code:
  282.  
  283.               printf("hey there ");
  284.  
  285.               main()
  286.               {
  287.                 printf("ho there ");
  288.               }
  289.  
  290.               printf("hi there ");
  291.  
  292.           would result in the output "hey there hi there ho there ".
  293.  
  294. 3.10. Unnecessary tokens
  295.  
  296.           If symbols are redundant, then they are usually unnecessary in
  297.           Cmm.  This allows for more flexibility in the non-C-trained and
  298.           also lets more code get in the tiny space available on the
  299.           command line.  Besides, I got tired of my compiler saying
  300.           "missing semi-colon"; What good is a semi-colon if it doesn't
  301.           tell the compiler anything new?  So you can have the statement
  302.           "foo()" as well as "foo();".  It certainly doesn't hurt to have
  303.           the semi-colon there, especially when it can clarify a "return;"
  304.           statement, for example, but it isn't necessary.  Similarly, "("
  305.           and ")" are often unnecessary, so you may have "while a < b a++"
  306.           as a complete statement.
  307.  
  308. 3.11. #include
  309.  
  310.           The #include statement has been enhanced for reading source
  311.           snippets from within other types of files.  So we have
  312.  
  313.               #Include <filespec,Extension,Prefix,HeaderLine,FooterLine>
  314.  
  315.           where filespec is the same as in C's #include <filespec>
  316.           statement, Extension is a file extension (such as BAT) that may
  317.           be added to the filespec (so batch files can say #include
  318.           <%0,bat>", Prefix specifies that only those lines in filespec
  319.           that begin with Prefix will be source, and HeaderLine and
  320.           FooterLine specify that source will be read only from sections of
  321.           filespec between HeaderLine and FooterLine.  If a full path is
  322.           not specified then CEnvi searches for the file in various paths
  323.           in this order:
  324.             *Search the current directory.
  325.             *If the code is run from a *.cmm source file, then search in
  326.               the source directory for the *.cmm file.
  327.             *If this is the Windows version of CEnvi, searches all the
  328.               files in the CMMPATH profile value (from WIN.INI in the
  329.               [CEnvi] section).
  330.             *Search all directories in the CMMPATH environment variable.
  331.             *Search the directory that CEnvi.exe is executed from.
  332.             *Search all directories from the PATH environment variable.
  333.  
  334. 3.12. Macros
  335.  
  336.           Function macros are not supported.  Since speed is not of primary
  337.           importance, a macro gains little over a function call, and so any
  338.           macros may simply become functions.
  339.  
  340.           Token replacement macros ("#define NULL 0") are supported in Cmm.
  341.  
  342. 3.13. Back-quote strings
  343.  
  344.           The back-quote character (`), also known as a "back-tick" or
  345.           "grave accent", can be used in Cmm in place of a double quote (")
  346.           to specify a string where escape sequences are not to be
  347.           translated.  So, for example, here are two ways to describe the
  348.           same file name:
  349.               "c:\\autoexec.bat"  // traditional method
  350.               `c:\autoexec.bat`   // alternative method
  351.  
  352. 3.14. Converting existing C code to Cmm
  353.  
  354.           Converting existing C code to Cmm, should you choose to do so, is
  355.           mostly a process of deleting unnecessary text.  You search on
  356.           type declarations, such as "int", "float", "struct", "char",
  357.           "[]", etc... and then delete these declaration strings.  For
  358.           instance, these instances of C code (or C++ code) on the left can
  359.           be replaced by the Cmm code on the right:
  360.  
  361.                    C                               Cmm     
  362.               ----------                      -------------
  363.  
  364.               int i;    ....................  i (or nothing at all)
  365.  
  366.               int foo = 3; .................. foo = 3;
  367.  
  368.               struct {    ................... /* nothing */
  369.                 int row;
  370.                 int col;
  371.               }
  372.  
  373.               char name[] = "George"; ....... name = "George";
  374.  
  375.               int goo(int a,char *s,int &c) . goo(a,s,c)
  376.  
  377.               int zoo[] = { 1, 2, 3 }; ...... zoo = { 1, 2, 3 };
  378.  
  379.           The next step in converting C to Cmm is to search for the address
  380.           and pointer operators ("*", "&").  If the '&' and '*' are
  381.           together so that the address of a variable is passed to a
  382.           function, then both of these operators become unnecessary because
  383.           Cmm passes lvars by reference.  If there are still "*" found then
  384.           they are usually referring to the zeroth value of a pointer
  385.           address, and so can be replaced with [0], as in "*foo = 4"
  386.           replaced by "foo[0] = 4".  Finally, the "->" operator for
  387.           structures must be replaced by "." either because the structure
  388.           is now being passed by referenced or because the element of the
  389.           structure is being referred to by its array index: "foo->row" may
  390.           need to become "foo[0].row".
  391.  
  392. -------------------------------- FILE LIST --------------------------------
  393. The CEnvi Unregistered Shareware package includes the files in the
  394. following list.  You are not permitted to upload or otherwise transfer
  395. copies of any registered version of CEnvi that does not include all of the
  396. files in this list.
  397.  
  398. *CENVI.EXE: CEnvi shareware executable for DOS, OS/2, or Windows.
  399. *CENVI2PM.EXE: Gateway program, executed trasnparently by CEnvi, for access
  400.   to PM-dependent system calls (OS/2 version only).
  401. *CENVI.DOC: CEnvi Shareware Manual, Chapter 1: CEnvi Unregistered Shareware
  402. *CMMTUTOR.DOC: CEnvi Shareware Manual, Chapter 2: Cmm Language Tutorial
  403. *CMM_VS_C.DOC: CEnvi Shareware Manual, Chapter 3: Cmm versus C, for C
  404.   Programmers
  405. *CENVILIB.DOC: CEnvi Shareware Manual, Chapter 4: Function Library
  406. *LICENSE.DOC: CEnvi Unregistered Shareware License Agreement
  407. *README.DOC: Introductory file. Read this first for quick intallation.
  408. *REGISTER.DOC: CEnvi registration form
  409. *INSTALL.CMM: Cmm source file for installing this shareware version
  410. * *.CMM, *.CMD, *.BAT, *.LIB: Many many sample programs using CEnvi.  See
  411.   CENVI.DOC for a complete list.
  412.  
  413. ----------------------------- REGISTRATION -------------------------------
  414. This is a shareware release.  Please register.  As a registered CEnvi user
  415. you will receive:
  416. *The latest version of CEnvi for all supported platforms (currently DOS,
  417.   OS/2, and Windows).
  418. *The CEnvi user's manual (almost 100 pages, including a description of the
  419.   Cmm programming language, a tutorial for those who have never programmed,
  420.   and descriptions and examples of the over 150 functions included in the
  421.   CEnvi library).
  422. *Free incremental electronic downloads for new versions of CEnvi for all
  423.   supported operating systems.
  424. *Unlimited support from Nombas and CEnvi/Cmm users through CompuServe
  425.   (72212,1622), internet (bsn@world.std.com), the cenvi-cmm e-mail mailing
  426.   list (cenvi-cmm@world.std.com), and the Nombas BBS
  427.   (ATDT16173916595,,,,,44444).
  428. *Access to the growing list of CEnvi utilities and libraries (some of which
  429.   are included in this unregistered shareware package, and others are
  430.   contributed by Nombas and CEnvi/Cmm users to the electronic locations
  431.   described above).
  432.  
  433. There are three ways to register CEnvi version 1.008: 
  434.  
  435. ***************************************************************************
  436. ********* REGISTRATION METHOD 1: CENVI MAIL-IN REGISTRATION FORM **********
  437. ***************************************************************************
  438. Please fill out and mail in this form, along with payment.
  439.  
  440. Where did you get CEnvi? ______________________________________________
  441.  
  442. Name: _________________________________________________________________
  443.  
  444. Company: ______________________________ Position: _____________________
  445.  
  446. Address: ______________________________________________________________
  447.  
  448. _______________________________________________________________________
  449.  
  450. ______________________________________________________________________
  451.  
  452. Country: _________________________   (add ZIP code if applicable)
  453.  
  454. Phone: ___________________________  EMail: ______________________________
  455.  
  456.           Diskette size: [  ] 3.5"   [  ] 5.25"
  457.  
  458. CEnvi Registered License & Manual.... Quantity _____ x $38.00 = $ _________
  459. License fee for additional CEnvi users at
  460. your organization (does not include additional
  461. manual or diskettes)...Simultaneous-User Count _____ x $15.00 = $ _________
  462. Additional CEnvi Manuals..............Quantity _____ x $10.00 = $ _________
  463. Shipping outside USA, Canada, or Mexico  $4.00 ................ $ _________
  464.                                                        Subtotal $ _________
  465. Massachusetts residents please add 5% sales tax ............... $ _________
  466.  
  467.                                                           Total $ _________
  468.  
  469. Include a check or money order for this total, IN U.S. FUNDS AND DRAWN ON A
  470. U.S. BANK, payable to Nombas, or supply the following credit card payment
  471. information.  Credit cards orders will be processed through a distributor:
  472. Custom Computer Systems of Medford, MA.
  473.  
  474. Credit card orders (circle one): MasterCard / Visa / American Express
  475.  
  476.                                  Discover / Carte Blanche / Diners Club
  477.  
  478.     Card Number _____________________________________  Expires ____________
  479.  
  480.     Exact name on card (print) ____________________________________________
  481.  
  482.     Signature (REQUIRED) __________________________________________________
  483.  
  484. Mail this form, along with payment or credit information, to:
  485.                Nombas
  486.                P.O. Box 875
  487.                Medford, MA  02155   USA
  488.  
  489.  
  490. ***************************************************************************
  491. ******** REGISTRATION METHOD 2: COMPUSERVE ELECTRONIC REGISTRATION ********
  492. ***************************************************************************
  493. CompuServe members may register directly through the CompuServe
  494. Registration Service.  To use this service enter GO SWREG at your CI$
  495. prompt.  Registration ID is 1354 for CEnvi for DOS, 1355 for CEnvi for
  496. OS/2, and 1356 for CEnvi for Windows (you only need to register ONE
  497. version).  Nombas will immediately be informed of your registration, and
  498. the CEnvi registration fee will automatically be added to your CompuServe
  499. bill.
  500.  
  501.  
  502. ***************************************************************************
  503. ************ REGISTRATION METHOD 3: Public (software) Library *************
  504. ***************************************************************************
  505. CREDIT CARD ORDERS ONLY -
  506.  
  507. You can order with MC, Visa, Amex, or Discover from Public (software)
  508. Library by calling 800-2424-PsL or 713-524-6394 or by FAX to 713-524-6398
  509. or by CIS Email to 71355,470. You can also mail credit card orders to PsL
  510. at P.O.Box 35705, Houston, TX 77235-5705.
  511.  
  512. THE ABOVE NUMBERS ARE FOR ORDERS ONLY.
  513.  
  514. Any questions about the status of the shipment of the order, refunds,
  515. registration options, product details, technical support, volume discounts,
  516. dealer pricing, site licenses, etc, must be directed to Nombas (see phone
  517. number and addresses below).
  518.  
  519. To insure that you get the latest version, PsL will notify Nombas the day
  520. of your order and we will ship the product directly to you.
  521.  
  522. CEnvi (all versions) is PsL product #11069.  Prices (including shipping and
  523. handling) are: $42 US/Canada and $45 overseas.
  524.  
  525.  
  526. ***************************************************************************
  527. Thank you for trying this shareware copy of CEnvi.  Mail inquires and other
  528. correspondences to:
  529.      Nombas
  530.      P.O. Box 875
  531.      Medford, MA  02155   USA
  532.  
  533. Nombas may also be contacted at:
  534.      Phone: (617)391-6595
  535.      Internet: bsn@world.std.com
  536.      CompuServe: 72212,1622
  537.      BBS: (617)391-6595 ext. 44 (e.g., ATDT16173916595,,,,,44444)
  538.